<?php
session_start();
if (!isset($_SESSION['admin'])) { header('Location: index.php'); exit; }
$passwdFile = 'passwd.txt';
$oldPass = file_exists($passwdFile) ? trim(file_get_contents($passwdFile)) : '123456';
if ($_POST) {
    if ($_POST['old'] !== $oldPass) {
        $msg = '原密码错误';
    } elseif ($_POST['new'] !== $_POST['new2']) {
        $msg = '两次新密码不一致';
    } elseif (!$_POST['new']) {
        $msg = '新密码不能为空';
    } else {
        file_put_contents($passwdFile, $_POST['new']);
        $msg = '密码修改成功';
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>修改密码</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container py-5" style="max-width:400px;">
    <h4 class="mb-4">修改密码</h4>
    <?php if(isset($msg)) echo '<div class="alert alert-'.($msg==="密码修改成功"?"success":"danger").'">'.$msg.'</div>'; ?>
    <form method="post" class="bg-white p-4 rounded shadow-sm">
        <div class="mb-3">
            <label class="form-label">原密码</label>
            <input name="old" type="password" class="form-control" required>
        </div>
        <div class="mb-3">
            <label class="form-label">新密码</label>
            <input name="new" type="password" class="form-control" required>
        </div>
        <div class="mb-3">
            <label class="form-label">确认新密码</label>
            <input name="new2" type="password" class="form-control" required>
        </div>
        <div class="d-flex justify-content-end gap-3 mt-4">
            <button type="submit" class="btn btn-primary btn-lg px-5">保存</button>
            <a href="dashboard.php" class="btn btn-outline-secondary btn-lg px-5">返回</a>
        </div>
    </form>
</div>
</body>
</html> 